﻿using Ewell.BizComponent.Service.IServer;
using Ewell.BizComponent.Service.Model.InterProxy;
using Ewell.BizComponent.Service.Model.Request;
using Ewell.BizComponent.Service.Model.Response;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.GateWay.Dto.Basic;
using Ewell.Interface.Common.Model.GateWay.Dto.Out;
using Ewell.Interface.Common.Model.GateWay.GateWayUriInfo;
using Ewell.Interface.Common.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Windows;

namespace Ewell.BizComponent.Service.Server
{
    /// <summary>
    /// 支付服务
    /// </summary>
    public class PaymentServer : IPaymentServer
    {
        const string interproxyProName = "bin\\plugin\\Ewell.Interface.Proxy.exe";
        /// <summary>
        /// 支付
        /// </summary>
        /// <param name="payWay"></param>
        /// <returns></returns>
        PaymentResp IPaymentServer.Pay(long interfaceId, PaymentReq paymentRequest)
        {
            //接口调用参数
            RequestHeader requestHeader = new RequestHeader()
            {
                interfaceId = interfaceId,
                tradeCode = "PAY101",
                employeeNo = LoginInfo.loginUser.no,
                employeeName = LoginInfo.loginUser.userName,
                deptNo = LoginInfo.loginUser.deptCode,
                deptName = LoginInfo.loginUser.deptName,
                zone = LoginInfo.loginUser.zone,
            };

            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(requestHeader),
                domain = JsonConvert.SerializeObject(paymentRequest),
            };

            InterfaceClientResult clientResult = default;
            try
            {
                clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            }
            catch (Exception ex)
            {
                throw new SpeedException(ex.Message);
            }
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            PaymentResponse result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<PaymentResponse>();
            else
                result = (PaymentResponse)clientResult?.@object;
            return new PaymentResp() { payTxnNo = result.payTxnNo };
        }
        /// <summary>
        /// 退款
        /// </summary>
        /// <param name="payWay"></param>
        /// <returns></returns>
        RefundResp IPaymentServer.Refund(long interfaceId, RefundReq refundRequest)
        {
            QueryChargeRecordV1Req queryChargeRecordV1Req = new QueryChargeRecordV1Req()
            {
                patientId = refundRequest.patientId,
                settlementIds = new List<string>() { refundRequest.refundHisBizNo },
                payStatuses = new List<string>() { "1" }
            };
            ChargeRecordDubboService chargeRecordDubboService = new ChargeRecordDubboService();
            var gateWayResult = chargeRecordDubboService.queryChargeRecordV1.Post<List<QueryChargeRecordV1Resp>>(queryChargeRecordV1Req).CheckState();
            if (gateWayResult.@object?.Count > 0)
            {
                MessageBox.Show("已经收费成功！不允许退支付!", "警告!", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
                throw new SpeedException("诊间收费", gateWayResult.message);
            }
            //接口调用参数
            RequestHeader requestHeader = new RequestHeader()
            {
                interfaceId = interfaceId,
                tradeCode = "PAY201",
                employeeNo = LoginInfo.loginUser.no,
                employeeName = LoginInfo.loginUser.userName,
                deptNo = LoginInfo.loginUser.deptCode,
                deptName = LoginInfo.loginUser.deptName,
                zone = LoginInfo.loginUser.zone,
            };

            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(requestHeader),
                domain = JsonConvert.SerializeObject(refundRequest),
            };
            InterfaceClientResult clientResult = default;
            try
            {
                clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            }
            catch (Exception ex)
            {
                throw new SpeedException(ex.Message);
            }
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            RefundResponse result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<RefundResponse>();
            else
                result = (RefundResponse)clientResult?.@object;
            return new RefundResp() { payTxnNo = result.payTxnNo, refundTxnNo = result.payTxnNo };
        }

        /// <summary>
        /// 查询余额
        /// </summary>
        /// <param name="payWay"></param>
        /// <returns></returns>
        QueryBalanceResp IPaymentServer.QueryBalance(long interfaceId, QueryBalanceReq queryBalanceReq)
        {
            //除了院内账户以外暂不支持
            if (interfaceId != 1435499783753404416)
                return new QueryBalanceResp();
            QueryAccountByOwnerNoRequest request = new QueryAccountByOwnerNoRequest()
            {
                accountOwnerNo = queryBalanceReq.accountOwnerNo,
                accountOwnerTypeCoding = "1",
                accountTypeCoding = "1",
            };
            AccountDubboService accountDubboService = new AccountDubboService();
            var gateWayResult = accountDubboService.queryAccountByOwnerNo.Post<QueryAccountByOwnerNoResponse>(request).CheckState();
            return new QueryBalanceResp() { accountBalance = gateWayResult.@object?.rechargeAccountVo?.accountBalance };
        }
        /// <summary>
        /// 获取支付金额
        /// </summary>
        /// <param name="interfaceId"></param>
        /// <param name="queryBalanceReq"></param>
        /// <returns></returns>
        QueryPayAmountResponse IPaymentServer.QueryPaymentAmount(long interfaceId, QueryPayAmountRequest queryPaymentAmount)
        {
            //接口调用参数
            RequestHeader requestHeader = new RequestHeader()
            {
                interfaceId = interfaceId,
                tradeCode = "PAY104",
                employeeNo = LoginInfo.loginUser.no,
                employeeName = LoginInfo.loginUser.userName,
                deptNo = LoginInfo.loginUser.deptCode,
                deptName = LoginInfo.loginUser.deptName,
                zone = LoginInfo.loginUser.zone,
            };

            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(requestHeader),
                domain = JsonConvert.SerializeObject(queryPaymentAmount),
            };

            InterfaceClientResult clientResult = default;
            try
            {
                clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            }
            catch (Exception ex)
            {
                throw new SpeedException(ex.Message);
            }
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            QueryPayAmountResponse result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<QueryPayAmountResponse>();
            else
                result = (QueryPayAmountResponse)clientResult?.@object;
            return result;
        }
    }
}
